package indi.kider.server.bill.service;

import indi.kider.common.model.apartment.Houses;
import indi.kider.common.model.bill.*;
import indi.kider.common.model.meter.CommandResponse;
import indi.kider.common.model.meter.InstantCommand;
import indi.kider.server.apartment.mapper.HousesMapper;
import indi.kider.server.bill.mapper.MeterMapper;
import indi.kider.server.bill.service.related.MeterClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.vankid.common.model.base.CommonResponse;

import java.util.List;

/**
 * Created by shixj on 2018/5/28.
 */
@SuppressWarnings("ALL")
@Service
public class MeterServiceImpl implements MeterService {
    @Autowired
    private MeterMapper meterMapper;
    @Autowired
    private MeterClientService meterClientService;
    @Autowired
    private HousesMapper housesMapper;

    @Override
    public Meter selectOne(Meter meter) throws Exception {
        Houses houses = housesMapper.selectByPrimaryKey(meter.getHousesId());
        Integer meterType = meter.getMeterType();
        if (meterType > 1){
            meter.setMeterCode(houses.getPowerMeter());
        }else {
            meter.setMeterCode(houses.getWaterMeter());
        }
        return meter;
    }

    @Override
    public CommonResponse<CommandResponse> meterControl(MeterControl meterControl) throws Exception {
        InstantCommand command = new InstantCommand();
        Meter query = new Meter();
        query.setHousesId(meterControl.getHousesId());
        query.setMeterType(meterControl.getMeterType());
        Meter meter = selectOne(query);
        if (meter == null) throw new Exception("房屋未安装该类表具！");
        command.setCld(meter.getLocationParam());
        command.setZddz(meter.getConcentrator());
        command.setGn("mwlhz");
        command.setNr(String.valueOf(meterControl.getOpType()));
        return meterClientService.sendCommand(command);
    }

    @Override
    public void update(MeterNameChange meterNameChange) {
        Meter meter = new Meter();
        meter.setId(meterNameChange.getId());
        meter.setName(meterNameChange.getName());
        meterMapper.updateByPrimaryKeySelective(meter);
    }

    @Override
    public List<RentMeter> getRentMeterList(RentMeter r) {
        return meterMapper.getRentMeterList(r);
    }

    @Override
    public List<MeterRecord> getMeterRecordList(RentMeter r) {
        return meterMapper.getMeterRecordList(r);
    }

    /**
     * 绑定表具
     *
     * @param meter
     */
    @Override
    @Transactional
    public void bindHouseMeter(Meter meter) throws Exception {
        Houses houses = new Houses();
        houses.setId(meter.getHousesId());
        if (meter.getMeterType() > 1) {
            houses.setPowerMeter(meter.getMeterCode());
        } else {
            houses.setWaterMeter(meter.getMeterCode());
        }
        housesMapper.updateByPrimaryKeySelective(houses);

//        CommonResponse<Meter> meterCommonResponse = meterClientService.getMeterDocument(meter);
//        if (meterCommonResponse == null || meterCommonResponse.getData() == null)
//            throw new Exception("未查询到表具，请检查表号是否正确！");
//        if (meterCommonResponse.doSuccess()) {
//            Meter qm = new Meter();
//            qm.setMeterCode(meter.getMeterCode());
//            qm.setMeterType(meter.getMeterType());
//            List<Meter> list = meterMapper.select(qm);
//            /* 已经挂表不让再挂表*/
//            if (null == list || list.isEmpty()) {
//                Meter query = new Meter();
//                query.setHousesId(meter.getHousesId());
//                query.setMeterType(meter.getMeterType());
//                List<Meter> meters = meterMapper.select(query);
//                if (meters != null && !meters.isEmpty())
//                    //替换
//                    meterMapper.delete(meters.get(0));
//                Meter meterNew = meterCommonResponse.getData();
//                meterNew.setHousesId(meter.getHousesId());
//                meterMapper.insert(meterNew);
//            } else {
//                Meter hasMeter = list.get(0);
//                HousesMsg msg = housesService.getHousesMsgById(hasMeter.getHousesId()).getData();
//                StringBuffer stringBuffer = new StringBuffer();
//                stringBuffer.append(msg.getCity()).append("|")
//                        .append(msg.getAreas()).append("|")
//                        .append(msg.getCommunity()).append("|")
//                        .append(msg.getHouseNumb());
//                throw new Exception("该表具号已经绑定，请确认【" + stringBuffer.toString() + "】");
//            }
//
//        } else
//            throw new Exception(meterCommonResponse.getMsg());

    }

    /**
     * 解除绑定表具
     *
     * @param meterId
     * @param housesId
     */
    @Override
    public void cleanHouseMeter(Long meterId, Long housesId) throws Exception {
        meterMapper.deleteByPrimaryKey(meterId);
    }
}
